package cn.sx.config;

/*
 *@program:sx_emall_ssm
 *@author: sx
 *@desc: 后台拦截器
 *@Time: 2020/9/16  10:16
 */

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Objects;

public class AdminInterceptor extends HandlerInterceptorAdapter {
    /**
     * 检测登陆状态
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        if (uri.contains("img") || uri.contains("css") || uri.contains("js") || uri.contains("login") || uri.contains("logout")) {
            return true;//不拦截路径
        }
        Object admin = request.getSession().getAttribute("admins");
        if(admin !=null && !admin.toString().trim().isEmpty()){
            return true;//验证通过
        }
        response.sendRedirect("login.jsp");
        return false;//其他情况一律拦截
    }
}
